package com.springcloud.eshop.product.service.impl;

import com.springcloud.eshop.entity.ProductIntro;
import com.springcloud.eshop.enums.DataTypeEnum;
import com.springcloud.eshop.enums.EventTypeEnum;
import com.springcloud.eshop.message.DataChangeMessage;
import com.springcloud.eshop.product.mapper.ProductIntroMapper;
import com.springcloud.eshop.product.service.ProductIntroService;
import com.springcloud.eshop.product.service.SendMQService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * (ProductIntro)表服务实现类
 *
 * @author makejava
 * @since 2020-06-29 05:49:46
 */
@Service("productIntroService")
public class ProductIntroServiceImpl extends SendMQService implements ProductIntroService {
    @Resource
    private ProductIntroMapper productIntroMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public ProductIntro queryById(Long id) {
        return this.productIntroMapper.queryById(id);
    }

    /**
     * 查询多条数据
     *
     * @param offset 查询起始位置
     * @param limit  查询条数
     * @return 对象列表
     */
    @Override
    public List<ProductIntro> queryAllByLimit(int offset, int limit) {
        return this.productIntroMapper.queryAllByLimit(offset, limit);
    }

    /**
     * 新增数据
     *
     * @param productIntro 实例对象
     * @return 实例对象
     */
    @Override
    public ProductIntro insert(ProductIntro productIntro) {
        this.productIntroMapper.insert(productIntro);
        sendMessage(new DataChangeMessage(EventTypeEnum.ADD.getType(), DataTypeEnum.PRODUCT_INTRO.getType(), productIntro.getId(), productIntro.getProductId()));
        return productIntro;
    }

    /**
     * 修改数据
     *
     * @param productIntro 实例对象
     * @return 实例对象
     */
    @Override
    public ProductIntro update(ProductIntro productIntro) {
        this.productIntroMapper.update(productIntro);
        sendMessage(new DataChangeMessage(EventTypeEnum.UPDATE.getType(), DataTypeEnum.PRODUCT_INTRO.getType(), productIntro.getId(), productIntro.getProductId()));
        return this.queryById(productIntro.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Long id) {
        int result = this.productIntroMapper.deleteById(id);
        sendMessage(new DataChangeMessage(EventTypeEnum.UPDATE.getType(), DataTypeEnum.PRODUCT_INTRO.getType(), id));
        return result > 0;
    }

    @Override
    public ProductIntro queryByProductId(long productId) {
        ProductIntro result = this.productIntroMapper.queryByProductId(productId);
        return result;
    }
}